<template>
	<right-layout :title="title" width="800px" :visible.sync="show" @closeLayout="closeLayout">
		<div class="layout-wrap">
			<el-form
				label-width="160px"
				label-position="left"
				label-suffix="："
				:model="ruleForm"
				:rules="rules"
				ref="form"
			>
				<el-form-item label="癌症分类" prop="aizheng_type">
					<el-input v-model="ruleForm.aizheng_type" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="癌症英文名" prop="aizheng_ename">
					<el-input v-model="ruleForm.aizheng_ename" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="癌症中文名" prop="aizheng_cname">
					<el-input v-model="ruleForm.aizheng_cname" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="对应器官英文名" prop="qiguan_ename">
					<el-input v-model="ruleForm.qiguan_ename" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="对应器官中文名" prop="qiguan_cname">
					<el-input v-model="ruleForm.qiguan_cname" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="ACSUSC" prop="ACSUSC">
					<el-input v-model="ruleForm.ACSUSC" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="DOSEFA" prop="DOSEFA">
					<el-input v-model="ruleForm.DOSEFA" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="DOSEFB" prop="DOSEFB">
					<el-input v-model="ruleForm.DOSEFB" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="CFRISK" prop="CFRISK">
					<el-input v-model="ruleForm.CFRISK" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="CIRISK" prop="CIRISK">
					<el-input v-model="ruleForm.CIRISK" placeholder="请输入"></el-input>
				</el-form-item>
				<el-form-item label="DDREFA" prop="DDREFA">
					<el-input v-model="ruleForm.DDREFA" placeholder="请输入"></el-input>
				</el-form-item>
			</el-form>
		</div>
		<div slot="foot">
			<div class="left">
				<el-button type="text" @click="closeLayout">关闭</el-button>
			</div>
			<el-button type="primary" :loading="loading" @click="submitFun(true)">保存</el-button>
			<!-- <el-button
				type="primary"
				:loading="loading"
				@click="submitFun(false)"
				v-if="!ruleForm.aizheng_id"
			>保存并新建</el-button>-->
			<!-- <el-button @click="resetForm">重置</el-button> -->
		</div>
	</right-layout>
</template>

<script>
import RightLayout from "@/components/common/comp/RightLayout"
import textLength from "@/components/textLength"
import selectPara from "@/components/selectPara"
import commCheckbox from "@/components/commCheckbox"
import { commPara } from "@/mixins/mixins"
import { mapState } from "vuex"

import { SS_AIZHENG_INSERT, SS_AIZHENG_UPDATE } from "@/service/hd5.js"

export default {
	name: "GdBrandClassifyAdd",
	props: {},
	components: {
		textLength,
		selectPara,
		commCheckbox,
		RightLayout,
	},
	mixins: [commPara],
	data() {
		var validate_descr = (rule, value, callback) => {
			if (this.ruleForm.descr_l > 256) {
				callback(new Error())
			} else {
				callback()
			}
		}
		return {
			config: {},
			title: "",
			show: false,
			ruleForm: {
				id: "",
				isOpen: false,
				aizheng_id: "",
				aizheng_type: "",
				aizheng_ename: "",
				aizheng_cname: "",
				qiguan_ename: "",
				qiguan_cname: "",
				ACSUSC: "",
				DOSEFA: "",
				DOSEFB: "",
				CFRISK: "",
				CIRISK: "",
				DDREFA: "",
			},
			rules: {
				name: [
					{
						type: "string",
						message: "请输入正确的字符串",
					},
					{
						required: true,
						message: "名称必须填写",
					},
					{
						pattern: /^[\w\W]{0,20}$/,
						message: "长度限制0-20位",
					},
				],
				openDt: [
					{
						required: true,
						message: "请选择开始时间",
						trigger: "blur",
					},
				],
				sort: [
					{
						pattern: /^[\w\W]{0,4}$/,
						message: "长度限制0-4位",
					},
				],
				descr: [
					{
						validator: validate_descr,
						message: "长度限制256字",
						trigger: "blur",
					},
				],
			},
			loading: false,
		}
	},
	computed: {
		...mapState({
			token: (state) => state.token.token,
		}),
	},
	mounted() {},
	methods: {
		showLayout(data) {
			if (data.aizheng_id) {
				this.ruleForm.aizheng_id = data.aizheng_id
				this.title = "修改"
				this.ruleForm.aizheng_type = data.aizheng_type
				this.ruleForm.aizheng_ename = data.aizheng_ename
				this.ruleForm.aizheng_cname = data.aizheng_cname
				this.ruleForm.qiguan_ename = data.qiguan_ename
				this.ruleForm.qiguan_cname = data.qiguan_cname
				this.ruleForm.ACSUSC = data.ACSUSC
				this.ruleForm.DOSEFA = data.DOSEFA
				this.ruleForm.DOSEFB = data.DOSEFB
				this.ruleForm.CFRISK = data.CFRISK
				this.ruleForm.CIRISK = data.CIRISK
				this.ruleForm.DDREFA = data.DDREFA
			} else {
				this.ruleForm.aizheng_id = ""
				this.title = "新增"
			}
			this.show = true
		},
		closeLayout() {
			this.show = false
			this.$refs.form.resetFields()
		},
		closeLayout1() {
			this.$refs.form.resetFields()
			this.disabled = false
		},
		// resetForm() {
		// 	this.$refs.form.resetFields()
		// },
		submitFun(flag) {
			this.$refs.form.validate((valid) => {
				if (valid) {
					if (this.ruleForm.aizheng_id) {
						let params = {
							aizheng_id: this.ruleForm.aizheng_id,
							aizheng_type: this.ruleForm.aizheng_type,
							aizheng_ename: this.ruleForm.aizheng_ename,
							aizheng_cname: this.ruleForm.aizheng_cname,
							qiguan_ename: this.ruleForm.qiguan_ename,
							qiguan_cname: this.ruleForm.qiguan_cname,
							ACSUSC: this.ruleForm.ACSUSC,
							DOSEFA: this.ruleForm.DOSEFA,
							DOSEFB: this.ruleForm.DOSEFB,
							CFRISK: this.ruleForm.CFRISK,
							CIRISK: this.ruleForm.CIRISK,
							DDREFA: this.ruleForm.DDREFA,
						} 
						this.loading = true

						this.$appApi
							.SS_AIZHENG_UPDATE(params)
							.then((res) => {
								var str = res.data.substring(0, 1)
								if (str == "E") {
									let message = res.data.substring(6)
									this.$notify.error({
										title: "修改失败！",
										message: message,
										type: "success",
									})
								} else if (str == "S") {
									this.$notify({
										title: "提示",
										type: "success",
										message: "修改成功!",
									})
									this.loading = false
									this.$emit("refresh")
									if (flag) {
										this.closeLayout()
									} else {
										this.closeLayout1()
									}
								}
							})
							.catch((err) => {
								console.log(err)
							})
					} else {
						let params = {
							aizheng_type: this.ruleForm.aizheng_type,
							aizheng_ename: this.ruleForm.aizheng_ename,
							aizheng_cname: this.ruleForm.aizheng_cname,
							qiguan_ename: this.ruleForm.qiguan_ename,
							qiguan_cname: this.ruleForm.qiguan_cname,
							ACSUSC: this.ruleForm.ACSUSC,
							DOSEFA: this.ruleForm.DOSEFA,
							DOSEFB: this.ruleForm.DOSEFB,
							CFRISK: this.ruleForm.CFRISK,
							CIRISK: this.ruleForm.CIRISK,
							DDREFA: this.ruleForm.DDREFA,
						}
						console.log(params)
						this.loading = true

						this.$appApi
							.SS_AIZHENG_INSERT(params)
							.then((res) => {
								var str = res.data.substring(0, 1)
								if (str == "E") {
									let message = res.data.substring(6)
									this.$notify.error({
										title: "新增失败！",
										message: message,
										type: "success",
									})
								} else if (str == "S") {
									this.$notify({
										title: "提示",
										type: "success",
										message: "新增成功!",
									})
									this.loading = false
									this.$emit("refresh")
									if (flag) {
										this.closeLayout()
									} else {
										this.closeLayout1()
									}
								}
							})
							.catch((err) => {
								console.log(err)
							})
					}
				}
			})
		},
	},
}
</script>